package com.ktjiaoyu.utils;

import java.util.ArrayList;
import java.util.List;

public class Page<T> {
    //当前页码
    private Integer pageNo = 1;
    //每页数量
    private Integer pageSize = 5;
    //总记录数
    private Integer totalCount = null;
    //总页数
    private Integer totalPageCount = 1;
    //查询结果
    private List<T> data;

    public Integer getPageNo() {
        return pageNo;
    }

    public void setPageNo(Integer pageNo) {
        if (pageNo <= 0) {
            pageNo = 1;
        }
        if (totalCount != null) {
            //计算过总页数后，当前页不能大于总页数
            if (totalPageCount >= 1 && pageNo > totalPageCount) {
                pageNo = totalPageCount;
            }
        }
        this.pageNo = pageNo;
    }

    public Integer getPageSize() {
        return pageSize;
    }

    public void setPageSize(Integer pageSize) {
        this.pageSize = pageSize;
    }

    public Integer getTotalCount() {
        return totalCount;
    }

    public void setTotalCount(Integer totalCount) {
        this.totalCount = totalCount;
        this.totalPageCount = this.totalCount % this.pageSize == 0 ? this.totalCount / this.pageSize : this.totalCount / this.pageSize + 1;
    }

    //计算偏移量
    public int offset() {
        return (this.pageNo - 1) * this.pageSize;
    }

    /**
     * 是否第一页
     */
    public boolean isFirst() {
        return this.pageNo == 1;
    }

    /**
     * 是否最后一页
     */
    public boolean isLast() {
        return this.pageNo == this.totalPageCount;
    }

    /**
     * 是否还有下一页
     */
    public boolean hasNext() {
        return this.pageNo < this.totalPageCount;
    }

    /**
     * 是否还有上一页
     */
    public boolean hasPrevious() {
        return this.pageNo > 1;
    }

    /**
     * 页面上显示指定长度页码
     * 自己理解:集合左边加到小于1  右边加到大于总页数 当集合长度大于等于指定长度时退出循环
     */
    public List<Integer> getShowPageNos(int showCount) {
        List<Integer> list = new ArrayList<Integer>();
        //先加中间页码
        list.add(pageNo);
        //每次页面计算的步长
        int step = 1;
        while (list.size() < showCount) {
            boolean added = false;
            int left = this.pageNo - step; //左边减页码
            int right = this.pageNo + step; //右边加页码
            if (left >= 1) {
                // 左边大于1  左边页码添加
                list.add(0, left);
                added = true;
            }
            if (right <= this.totalPageCount) {
                // 右边小于总页数 右边页码添加
                list.add(right);
                added = true;
            }
            if (!added) {
                //如果左右都没有添加成功,则退出循环
                break;
            }
            //每次循环步长加1
            step++;
        }
        return list;
    }

    public Integer getTotalPageCount() {
        return totalPageCount;
    }

    public void setTotalPageCount(Integer totalPageCount) {
        this.totalPageCount = totalPageCount;
    }

    public List<T> getData() {
        return data;
    }

    public void setData(List<T> data) {
        this.data = data;
    }
}
